################################################################################
# Legislature Size and Party Unity   ###################################
# Jaclyn Kaslovsky and Michael Olson ###########################################
################################################################################

################################################################################
# working directory, packages and functions ####################################
################################################################################

#Set a working directory to output all results. 
#setwd("~/Replication Package/")
#Put data in a subfolder called "Data". 
#Make an empty subfolder called "Paper" that contains another subfolder called "figures_tables".

#load packages
require(lfe)
require(stargazer)
require(ggplot2)
require(readxl)
require(dplyr)
require(starpolishr)
require(tidyr)
require(english)
require(haven)
require(DataCombine)

################################################################################  
# Load in the Data  ############################################################
################################################################################

dat_ilin <- read.csv("Data/ilin_data.csv")

dat_njvt <- read.csv("Data/njvt_data.csv")

dubin <- read.csv("Data/dubin_excerpt.csv")

################################################################################
# Descriptive Information and Summary Stats ####################################
################################################################################

# what is the median size of legislatures during this time period?

  writeLines(as.character(median(dubin$lht,na.rm=T)),"./Paper/figures_tables/mediansize.tex")

# create the lagged size variable and the change

  dubin <- dubin %>% arrange(state,year)
  dubin <- DataCombine::slide(Var="lht",NewVar="lht_lag",GroupVar="state",TimeVar="year",slideBy = -1,data=dubin)
  dubin$change <- dubin$lht-dubin$lht_lag
  dubin <- dubin[!is.na(dubin$change),]

# create indicators for changes of 10 or 5
  
  dubin$bigchange10 <- ifelse(abs(dubin$change)>=10,1,0)
  dubin$bigchange5 <- ifelse(abs(dubin$change)>=5,1,0)

# how many changes of these sizes do states have?

  ag_changes <- as.data.frame(dubin %>% group_by(state) %>% summarize(big_changes10=sum(bigchange10,na.rm=T),
                                                                      big_changes5=sum(bigchange5,na.rm=T)))

# how many total? how many on average across states?

  writeLines(as.character(sum(ag_changes$big_changes10,na.rm=T)),"./Paper/figures_tables/changesof10.tex")
  writeLines(as.character(sum(ag_changes$big_changes5,na.rm=T)),"./Paper/figures_tables/changesof5.tex")
  
  writeLines(as.character(english(median(ag_changes$big_changes10))),"./Paper/figures_tables/median10.tex")
  writeLines(as.character(english(median(ag_changes$big_changes5))),"./Paper/figures_tables/median5.tex")
  
  bc10 <- dubin[dubin$bigchange10==1,]
  writeLines(as.character(nrow(bc10[bc10$change>0 & !is.na(bc10$change),])),"./Paper/figures_tables/changesof10positive.tex")
  writeLines(as.character(nrow(bc10[bc10$change<0 & !is.na(bc10$change),])),"./Paper/figures_tables/changesof10negative.tex")

# IL/IN party unity score density plot ##############################################

dat_ilin$chamber_size <- ifelse(dat_ilin$bigger==1,"Larger","Smaller")

ggplot(dat_ilin, aes(x=partyunity, fill=chamber_size,colour=chamber_size)) + 
  geom_density(alpha=.5)+
  scale_color_grey(end=0.7)+scale_fill_grey(end=0.7)+
  theme_bw()+
  ylab("Density")+xlab("Party Unity Score")+
  labs(fill="Chamber Size",colour="Chamber Size") + 
  facet_wrap(~State)+
  theme(strip.background = element_rect(fill="white"),
        strip.text = element_text(face="bold",size=10))

ggsave("./Paper/figures_tables/Figure2.eps",device=cairo_ps,width=6,height=2.5,units="in")

# graphical presentation of IL/IN results ############################################

ggplot(data=dat_ilin,aes(x=year,y=partyunity,group=interaction(State,bigger3),colour=State))+
  geom_point(stat="summary",fun.y="mean",size=2.5)+
  geom_line(stat="summary",fun.y="mean",size=1)+
  xlab("Year")+ylab("Party Unity")+
  geom_vline(xintercept=1841,colour="indianred",linetype=2,size=1.25)+
  geom_vline(xintercept=1847,colour="indianred",linetype=2,size=1.25)+
  scale_colour_grey(end=0.7)+
  theme_minimal()+
  theme(legend.position = "bottom")

ggsave("./Paper/figures_tables/Figure3.eps",device=cairo_ps,width=6,height=3,units="in")

# NJ/VT party unity score density plot ##############################################

dat_njvt$chamber_size <- ifelse(dat_njvt$bigger==1,"Larger","Smaller")

ggplot(dat_njvt, aes(x=partyunity, fill=chamber_size,colour=chamber_size)) + 
  geom_density(alpha=.5)+
  scale_color_grey(end=0.7)+scale_fill_grey(end=0.7)+
  theme_bw()+
  ylab("Density")+xlab("Party Unity Score")+
  labs(fill="Chamber Size",colour="Chamber Size") + facet_wrap(~State)+
  theme(strip.background = element_rect(fill="white"),
        strip.text = element_text(face="bold",size=10))

ggsave("./Paper/figures_tables/Figure4.eps",device=cairo_ps,width=6,height=2.5,units="in")

################################################################################  
# Illinois and Indiana Analysis ################################################  
################################################################################

# main regression table ########################################################

ilin_state <- felm(partyunity~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int <- felm(partyunity~bigger*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_id <- felm(partyunity~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)
ilin_id_int <- felm(partyunity~bigger*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)

ilin_state_cont <- felm(partyunity~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int_cont <- felm(partyunity~lht*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_id_cont <- felm(partyunity~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)
ilin_id_int_cont <- felm(partyunity~lht*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)

ilinsg <-  stargazer(ilin_state,ilin_state_int,ilin_id,ilin_id_int,
                     dep.var.labels = c("Party Unity Score"),
                     order=c(1,5,2,3,4),
                     covariate.labels = c("Chamber Larger",
                                          "Larger $\\times$ In Maj.",
                                          "In Majority",
                                          "Majority Seat Share", "Incumbent"),
                     keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                     add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                        "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                     title = "Party Unity and Legislature Size: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab",
                     notes.append = FALSE,notes.label = "", no.space=T,
                     digits=3,digits.extra=3,
                     omit.table.layout = "l#",
                     star.char=c("*","**"),star.cutoffs = c(.1,.05),
                     notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_cont <-  stargazer(ilin_state_cont,ilin_state_int_cont,ilin_id_cont,ilin_id_int_cont,
                          dep.var.labels = c("Party Unity Score"),
                          order=c(1,5,2,3,4),
                          covariate.labels = c("Chamber Size",
                                               "Size $\\times$ In Maj.",
                                               "In Majority",
                                               "Majority Seat Share", "Incumbent"),
                          keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                          add.lines = list(c("Fixed Effects","State + Year","State + Year","Leg. + Year","Leg. + Year")),
                          title = "Party Unity and Legislature Size: Illinois and Indiana, 1838 to 1851  \\vspace{-0.75em}",label="ilintab",
                          notes.append = FALSE,notes.label = "", no.space=T,
                          digits=3,digits.extra=3,
                          omit.table.layout = "l#",
                          star.char=c("*","**"),star.cutoffs = c(.1,.05),
                          notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

inil_panels <- star_panel(ilinsg,ilinsg_cont,panel.label.fontface="bold",
                          panel.names = c("Binary Independent Variable", 
                                          "Continuous Independent Variable"),
                          same.summary.stats = T,
                          same.lhs.vars = F)
cat(inil_panels, sep = '\n', file = "./Paper/figures_tables/Table1.tex")

# mechanism table ##############################################################

# within-district models 

ilin_dist <- felm(partyunity~bigger+majority+maj_share+inc|county+mod_year|0|ko_id,data=dat_ilin)
ilin_distid <- felm(partyunity~bigger+majority+maj_share+inc|id_county+mod_year|0|ko_id,data=dat_ilin)

ilin_dist_cont <- felm(partyunity~lht+majority+maj_share+inc|county+mod_year|0|ko_id,data=dat_ilin)
ilin_distid_cont <- felm(partyunity~lht+majority+maj_share+inc|id_county+mod_year|0|ko_id,data=dat_ilin)

# single-county sample models

dat_ilin$out <- dat_ilin$partyunity

ilin_single <- felm(out~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1,])
ilin_id_single <- felm(out~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1,])

ilin_single_cont <- felm(out~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1,])
ilin_id_single_cont <- felm(out~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1,])

mechsg <-  stargazer(ilin_dist,ilin_distid,ilin_single,ilin_id_single,
                     dep.var.caption = c("Party Unity Score"),
                     dep.var.labels=c("Within District Models","Single IL County Sample"),
                     covariate.labels = c("Chamber Larger",
                                          "In Majority",
                                          "Majority Seat Share", "Incumbent"),
                     keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                     add.lines = list(
                       c("Fixed Effects","Year + District","Year + Leg.-District","Year + State","Year + Leg.")),
                     title = "Exploring Theoretical Mechanisms: Illinois and Indiana, 1838 to 1851  \\vspace{-0.75em}",label="mechtest",
                     notes.append = FALSE,notes.label = "", no.space=T,
                     digits=3,digits.extra=3,column.sep.width = "0pt",
                     omit.table.layout = "l#",
                     star.char=c("*","**"),star.cutoffs = c(.1,.05),
                     notes="\\parbox[t]{0.95\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

mechsg_cont <-  stargazer(ilin_dist_cont,ilin_distid_cont,ilin_single_cont,ilin_id_single_cont,
                          dep.var.caption = c("Party Unity Score"),
                          dep.var.labels=c("Within District Models","Single IL Member $+$ County Sample"),
                          covariate.labels = c("Chamber Size",
                                               "In Majority",
                                               "Majority Seat Share", "Incumbent"),
                          keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                          add.lines = list(
                            c("Fixed Effects","Year + District","Year + Leg.-District","Year + State","Year + Leg.")),
                          title = "Exploring Theoretical Mechanisms: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="mechtest",
                          notes.append = FALSE,notes.label = "", no.space=T,
                          digits=3,digits.extra=3,column.sep.width = "0pt",
                          omit.table.layout = "l#",
                          star.char=c("*","**"),star.cutoffs = c(.1,.05),
                          notes="\\parbox[t]{0.8\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

mech_panels <- star_panel(mechsg,mechsg_cont,panel.label.fontface="bold",
                          panel.names = c("Binary Independent Variable", 
                                          "Continuous Independent Variable"),
                          same.lhs.vars = F,same.summary.stats = T)
cat(mech_panels, sep = '\n', file = "./Paper/figures_tables/Table2.tex")

################################################################################
# New Jersey and Vermont Analysis ##############################################
################################################################################

# main regression analyses 

njvt2 <- felm(partyunity~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvtint2 <- felm(partyunity~bigger*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvt3 <- felm(partyunity~bigger+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)
njvtint1 <- felm(partyunity~bigger*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)

njvt2_cont <- felm(partyunity~lht+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvtint2_cont <- felm(partyunity~lht*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvt3_cont <- felm(partyunity~lht+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)
njvtint1_cont <- felm(partyunity~lht*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)

njvtsg <-  stargazer(njvt2,njvtint2,njvt3,njvtint1,
                     dep.var.labels = c("Party Unity Score"),
                     order=c(1,5,2,3,4),
                     covariate.labels = c("Chamber Larger",
                                          "Larger $\\times$ In Maj.",
                                          "In Majority",
                                          "Majority Seat Share", "Incumbent"),
                     keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                     add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                     title = "Party Unity and Legislature Size: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab",
                     notes.append = FALSE,notes.label = "", no.space=T,
                     #table.layout ="-ld-#-t-as-n",
                     omit.table.layout = "l#",
                     digits=3,digits.extra=3,
                     star.char=c("*","**"),star.cutoffs = c(.1,.05),
                     notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_cont <-  stargazer(njvt2_cont,njvtint2_cont,njvt3_cont,njvtint1_cont,
                          dep.var.labels = c("Party Unity Score"),
                          order=c(1,5,2,3,4),
                          covariate.labels = c("Chamber Size",
                                               "Size $\\times$ In Maj.",
                                               "In Majority",
                                               "Majority Seat Share", "Incumbent"),
                          keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                          add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                          title = "Party Unity and Legislature Size: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab",
                          notes.append = FALSE,notes.label = "", no.space=T,
                          #table.layout ="-ld-#-t-as-n",
                          omit.table.layout = "l#",
                          digits=3,digits.extra=3,
                          star.char=c("*","**"),star.cutoffs = c(.1,.05),
                          notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  

njvt_panels <- star_panel(njvtsg,njvtsg_cont,panel.label.fontface="bold",
                          panel.names = c("Binary Independent Variable", 
                                          "Continuous Independent Variable"),
                          same.lhs.vars = F,same.summary.stats = T)
cat(njvt_panels, sep = '\n', file = "./Paper/figures_tables/Table3.tex")
